package com.yongcheon.sms_manager.ui.phone_spam.list;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.QuickContactBadge;
import android.widget.TextView;

import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.data.contract.ContactData;
import com.yongcheon.sms_manager.data.phone_spsm.AbstractPhoneData;
import com.yongcheon.sms_manager.utils.DateUtil;
import com.yongcheon.sms_manager.vo.ContactVO;
import com.yongcheon.sms_manager.vo.PhoneVO;

/**
 * Created by yongcheon on 2014-11-20.
 */
public class PhoneCursorAdapter extends CursorAdapter {
    LayoutInflater Inflater;
    AbstractPhoneData aData;
    ContactData contactData;

    public void setPhoneService(AbstractPhoneData aData){
        this.aData = aData;
    }
    public void setContactData(ContactData contactData){
        this.contactData = contactData;
    }

    public PhoneCursorAdapter(Context context, Cursor c) {
        super(context, c);
        Inflater = LayoutInflater.from(context);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View newView = Inflater.inflate(R.layout.spam_list, parent, false);
        return newView;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        //contactData.refresh(view.getContext().getContentResolver()); // 주소록 신규 등록 시 처리
        PhoneVO vo = aData.createPhoneVOFromCursor(cursor);
        ContactVO contactvo = contactData.getContactByPhoneNum(vo.getNumber());

        String from = getFrom(vo, context);

        TextView represent = (TextView)view.findViewById(R.id.represent);
        represent.setText(from);

        TextView title = (TextView)view.findViewById(R.id.title);
        title.setText(vo.getNumber());

        TextView date = (TextView)view.findViewById(R.id.date);
        date.setText(DateUtil.formatTimeStampString(context, vo.getDate(), false));
        TextView type = (TextView)view.findViewById(R.id.spam_type);
        type.setText(getReceiveType(context, vo.getType()));

        QuickContactBadge badge = (QuickContactBadge)view.findViewById(R.id.avatar);
        updateAvatarView(contactvo, vo, badge, context);

        setLonclickListener(view, vo, contactvo);
    }

    private void updateAvatarView(ContactVO contactvo, PhoneVO vo, QuickContactBadge badge, Context context){
        Drawable avatarDrawable = context.getResources().getDrawable(R.drawable.ic_contact_picture);
        if(contactvo != null) {
            Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactvo.getContactid());
            badge.assignContactUri(contactUri);
            avatarDrawable = contactData.getAvatarDrawable(contactvo, context);
        }else{
            badge.assignContactFromPhone(vo.getNumber(), true);
            avatarDrawable = context.getResources().getDrawable(R.drawable.ic_contact_picture);
        }
        badge.setImageDrawable(avatarDrawable);
    }

    private String getFrom(PhoneVO vo, Context context){
        ContactVO contactvo = contactData.getContactByPhoneNum(vo.getNumber());
        String from = contactData.getFromOnlyName(contactvo, context);
        return from;
    }

    private void setLonclickListener(View view, PhoneVO vo, ContactVO contactvo){
        View.OnLongClickListener longClickListener = new PhoneLongClickListener(vo, contactvo, view);
        view.setOnLongClickListener(longClickListener);
    }

    public String getReceiveType(Context context, int type){
        String returnStr = "";
        switch (type){
            case 1 :
                returnStr = context.getResources().getString(R.string.receive);
            break;
            case 3 :
                returnStr = context.getResources().getString(R.string.out_call);
            break;
            case 5 :
                returnStr = context.getResources().getString(R.string.block_receive);
            break;
            default:
                returnStr = context.getResources().getString(R.string.out_call);
            break;
        }
        return returnStr;
    }

}
